package com.xinxuan.contentcenter.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinxuan.contentcenter.base.BaseResult;

import lombok.extern.slf4j.Slf4j;

/**
 * 全局异常统一处理
 *
 * @author zhoulei
 * @date 2021/4/8 18:50
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理缺失参数异常异常
     *
     * @param request
     *            HttpServletRequest
     * @param e
     *            MissingServletRequestParameterException
     * @return BaseResult
     */
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    @ResponseBody
    public BaseResult missingParameterExceptionHandler(HttpServletRequest request,
        MissingServletRequestParameterException e) {
        log.error("catchMissingServletRequestParameterException, requestURI={}", request.getRequestURI(), e);
        return BaseResult.newErrorResult(BaseResult.FAILED, "参数" + e.getParameterName() + "缺失");
    }

    /**
     * 处理自定义的业务异常
     *
     * @param request
     *            HttpServletRequest
     * @param e
     *            BusinessException
     * @return BaseResult
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public BaseResult bizExceptionHandler(HttpServletRequest request, BusinessException e) {
        log.warn("catchBusinessException, requestURI={},exception={}", request.getRequestURI(), e.getMessage());
        return BaseResult.newErrorResult(BaseResult.FAILED, e.getMessage());
    }

    @ExceptionHandler(SecurityException.class)
    @ResponseBody
    public BaseResult securityExceptionHandler(HttpServletRequest request, SecurityException e) {
        log.warn("catchBusinessException, requestURI={},exception={}", request.getRequestURI(), e.getMessage());
        return BaseResult.newErrorResult(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
    }

    /**
     * 处理空指针的异常
     *
     * @param request
     *            HttpServletRequest
     * @param e
     *            NullPointerException
     * @return BaseResult
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public BaseResult nullPointerExceptionHandler(HttpServletRequest request, NullPointerException e) {
        log.error("catchNullPointerException, requestURI={}", request.getRequestURI(), e);
        return BaseResult.newErrorResult(BaseResult.FAILED, "未知错误");
    }

    /**
     * 处理其他异常
     *
     * @param request
     *            HttpServletRequest
     * @param e
     *            Exception
     * @return BaseResult
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResult exceptionHandler(HttpServletRequest request, Exception e) {
        log.error("catchException, requestURI={}", request.getRequestURI(), e);
        return BaseResult.newErrorResult(BaseResult.FAILED, "未知错误");
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public BaseResult handle(MethodArgumentNotValidException exception) {
        log.warn("参数异常信息：", exception);
        StringBuffer message = new StringBuffer();
        exception.getBindingResult().getAllErrors().forEach((error) -> {
            String errorMessage = error.getDefaultMessage();
            message.append(errorMessage).append(",");
        });
        String result = message.toString();
        return BaseResult.newErrorResult(result.substring(0, result.length() - 1));
    }
}
